package com.czwj.action;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.czwj.entities.Lettertext;
import com.czwj.entities.OperateResult;
import com.czwj.entities.UserInfo;
import com.czwj.service.LettertextService;
import com.czwj.service.UserInfoService;

@Controller
public class LettertextAction{
	@Resource
	LettertextService lettertextService;
	
	@Resource
	UserInfoService userInfoService;
	
	// 获取新消息数量
	// 判断角色
	@RequestMapping(value = "/getRoleid",method = RequestMethod.GET)
	@ResponseBody
	public Integer getRoleid(HttpServletRequest request,HttpServletResponse response){
		// HttpSession session = request.getSession();
		// Object userid = session.getAttribute("key");
		// String username = userid.toString().substring(3,userid.toString().length());
		// UserInfo user = userInfoService.findByUserName(username);
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		int roleid = userInfo.getRoleid();
		return roleid;
	}
	
	@RequestMapping(value = "/newMsg",method = RequestMethod.GET)
	@ResponseBody
	public Integer newMsgNum(HttpServletRequest request,HttpServletResponse response){
		// HttpSession session = request.getSession();
		// Object userid = session.getAttribute("key");
		// String username;
		// try{
		// username = userid.toString().substring(3,userid.toString().length());
		// }
		// catch(NullPointerException e){
		// return 0;
		// }
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		List<Lettertext> newMsg = lettertextService.selectByState(userInfo.getUsername());
		return newMsg.size();
	}
	
	// 获取消息列表
	@RequestMapping(value = "/getMsg",method = RequestMethod.GET)
	@ResponseBody
	public Map getMsg(HttpServletRequest request,HttpServletResponse response){
		// HttpSession session = request.getSession();
		// Object userid = session.getAttribute("key");
		// String username = userid.toString().substring(3,userid.toString().length());
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		List<Lettertext> allMsg = lettertextService.selectLetterList(userInfo.getUsername());
		Map<Integer,Object> result = new LinkedHashMap<Integer,Object>();
		for(int i = 0;i < allMsg.size();i++){
			Lettertext Msg = allMsg.get(i);
			result.put(i,Msg);
		}
		return result;
	}
	
	// 获取消息列表
	@RequestMapping(value = "/getSendMsg",method = RequestMethod.GET)
	@ResponseBody
	public Map getSendMsg(HttpServletRequest request,HttpServletResponse response){
		// HttpSession session = request.getSession();
		// Object userid = session.getAttribute("key");
		// String username = userid.toString().substring(3,userid.toString().length());
		// UserInfo user = userInfoService.findByUserName(username);
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		List<Lettertext> allMsg = lettertextService.selectSendMsg(userInfo.getId());
		Map<Integer,Object> result = new LinkedHashMap<Integer,Object>();
		for(int i = 0;i < allMsg.size();i++){
			Lettertext Msg = allMsg.get(i);
			result.put(i,Msg);
		}
		return result;
	}
	
	// 查看具体消息
	@RequestMapping(value = "/Msg",method = RequestMethod.GET)
	@ResponseBody
	public Object msg(@RequestParam("id") Integer id){
		Lettertext msg = lettertextService.selectByPrimaryKey(id);
		if(msg.getState() == 1){
			Lettertext updataState = new Lettertext();
			updataState.setId(id);
			updataState.setState(2);
			int count = lettertextService.updateByPrimaryKeySelective(updataState);
		}
		return msg;
	}
	
	// 删除指定消息
	@RequestMapping(value = "/deletemail.do",method = RequestMethod.DELETE)
	@ResponseBody
	public OperateResult deletemail(@RequestParam("id") Integer id){
		OperateResult result = new OperateResult();
		int count = lettertextService.deleteByPrimaryKey(id);
		result.setSuccess(count > 0);
		return result;
	}
}
